/**
 * OrderModel
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

/**
 * 
 * 订单详情model
 * 
 * @version [版本号, 2012-9-17]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author ryg66007
 */
public class OrderModel implements Serializable {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 58638010312047709L;

	/**
	 * 订单号
	 */
	private String OrderNumber;

	private String CustomerID;

	private String SalesChannelType;

	private String OrderDate;

	private double GrandTotal;

	private double ShippingFee;
	
	private double AllDiscountTotal;

	private String CurrencyCode;

	private String CurrencyCodeStr;

	private String OrderStatus;

	private String PaymentStatus;

	private String LockStatus;

	private String ShippingMethod;

	private String Light;

	private ShipAddress ShippingAddress;

	private List<Liness> Lines;

	private List<Liness> giftLines;
	private boolean CanCancel;
	private boolean CanComplete;
	private boolean CanModifyShippingAddress;
	private boolean CanBeginEdit;
	private boolean CanEndEdit;
	private boolean IsPaymentCompleted;
	private boolean CanRepay;
	private boolean IsCompleted;
	private boolean IsFullShipped;
	private boolean IsShipping;

	private double DueAmount;

	private String OrderStatusStr;

	private boolean IsCancelled;
	
	//应收运费
	private double ReceivableShippingFee;
	//运费折扣
	private double ShippingFeeDiscount;
	
	//物流信息
	private ArrayList<Package> packages;

	private float ExpectReturnSCAmount;
	
	public float getExpectReturnSCAmount() {
        return ExpectReturnSCAmount;
    }

    public void setExpectReturnSCAmount(float expectReturnSCAmount) {
        ExpectReturnSCAmount = expectReturnSCAmount;
    }

    public ArrayList<Package> getPackages() {
		return packages;
	}

	public void setPackages(ArrayList<Package> packages) {
		this.packages = packages;
	}

	public double getReceivableShippingFee() {
		return ReceivableShippingFee;
	}

	public void setReceivableShippingFee(double receivableShippingFee) {
		ReceivableShippingFee = receivableShippingFee;
	}

	public double getShippingFeeDiscount() {
		return ShippingFeeDiscount;
	}

	public void setShippingFeeDiscount(double shippingFeeDiscount) {
		ShippingFeeDiscount = shippingFeeDiscount;
	}

	public static class ShipAddress implements Serializable {
		private static final long serialVersionUID = 4951483145560862792L;

		private String FirstName;

		private String LastName;

		private String PhoneNumber;

		/**
		 * 供创建订单接口使用 由于不同系统字段没有统一
		 */
		private String Phone;

		private String Email;

		private String Mobile;

		private String PostalCode;

		private String Street1;

		private String Street2 = "";

		private String City;

		private String State;

		private String Country;

		public String getEmail() {
			return Email;
		}

		public void setEmail(String email) {
			Email = email;
		}

		public String getMobile() {
			return Mobile;
		}

		public void setMobile(String mobile) {
			Mobile = mobile;
		}

		public String getFirstName() {
			return FirstName;
		}

		public void setFirstName(String firstName) {
			FirstName = firstName;
		}

		public String getLastName() {
			return LastName;
		}

		public void setLastName(String lastName) {
			LastName = lastName;
		}

		public String getPhoneNumber() {
			return PhoneNumber;
		}

		public void setPhoneNumber(String phoneNumber) {
			PhoneNumber = phoneNumber;
		}

		public String getPhone() {
			return Phone;
		}

		public void setPhone(String phone) {
			Phone = phone;
		}

		public String getPostalCode() {
			return PostalCode;
		}

		public void setPostalCode(String postalCode) {
			PostalCode = postalCode;
		}

		public String getStreet1() {
			return Street1;
		}

		public void setStreet1(String street1) {
			Street1 = street1;
		}

		public String getStreet2() {
			return Street2;
		}

		public void setStreet2(String street2) {
			Street2 = street2;
		}

		public String getCity() {
			return City;
		}

		public void setCity(String city) {
			City = city;
		}

		public String getState() {
			return State;
		}

		public void setState(String state) {
			State = state;
		}

		public String getCountry() {
			return Country;
		}

		public void setCountry(String country) {
			Country = country;
		}

		@Override
		public String toString() {
			return "ShipAddress [FirstName=" + FirstName + ", LastName="
					+ LastName + ", PhoneNumber=" + PhoneNumber + ", Email="
					+ Email + ", Mobile=" + Mobile + ", PostalCode="
					+ PostalCode + ", Street1=" + Street1 + ", Street2="
					+ Street2 + ", City=" + City + ", State=" + State
					+ ", Country=" + Country + ", Phone=" + Phone + "]";
		}

	}

	public static class Liness implements Serializable {
		private static final long serialVersionUID = -7206559416447988211L;

		private int GiftQty;

		private String Notes;

		private int OrderedQty;

		private int Sku;

		private double UnitCost;

		private double UnitPrice;

		private String Name;

		private String SmileImageUrl;
		
		private boolean isGift;
		
		

		public boolean isGift() {
			return isGift;
		}

		public void setGift(boolean isGift) {
			this.isGift = isGift;
		}

		public int getGiftQty() {
			return GiftQty;
		}

		public void setGiftQty(int giftQty) {
			GiftQty = giftQty;
		}

		public String getNotes() {
			return Notes;
		}

		public void setNotes(String notes) {
			Notes = notes;
		}

		public int getOrderedQty() {
			return OrderedQty;
		}

		public void setOrderedQty(int orderedQty) {
			OrderedQty = orderedQty;
		}

		public int getSku() {
			return Sku;
		}

		public void setSku(int sku) {
			Sku = sku;
		}

		public double getUnitCost() {
			return UnitCost;
		}

		public void setUnitCost(double unitCost) {
			UnitCost = unitCost;
		}

		public double getUnitPrice() {
			return UnitPrice;
		}

		public void setUnitPrice(double unitPrice) {
			UnitPrice = unitPrice;
		}

		public String getName() {
			return Name;
		}

		public void setName(String name) {
			Name = name;
		}

		public String getSmileImageUrl() {
			return SmileImageUrl;
		}

		public void setSmileImageUrl(String smileImageUrl) {
			SmileImageUrl = smileImageUrl;
		}

	}

	public boolean isIsCancelled() {
		return IsCancelled;
	}

	public void setIsCancelled(boolean isCancelled) {
		IsCancelled = isCancelled;
	}

	public boolean isIsPaymentCompleted() {
		return IsPaymentCompleted;
	}

	public void setIsPaymentCompleted(boolean isPaymentCompleted) {
		IsPaymentCompleted = isPaymentCompleted;
	}

	public boolean isCanRepay() {
		return CanRepay;
	}

	public void setCanRepay(boolean canRepay) {
		CanRepay = canRepay;
	}

	public double getDueAmount() {
		return DueAmount;
	}

	public void setDueAmount(double dueAmount) {
		DueAmount = dueAmount;
	}

	public boolean isIsFullShipped() {
		return IsFullShipped;
	}

	public void setIsFullShipped(boolean isFullShipped) {
		IsFullShipped = isFullShipped;
	}

	public boolean isIsShipping() {
		return IsShipping;
	}

	public void setIsShipping(boolean isShipping) {
		IsShipping = isShipping;
	}

	public boolean isIsCompleted() {
		return IsCompleted;
	}

	public void setIsCompleted(boolean isCompleted) {
		IsCompleted = isCompleted;
	}

	public String getOrderStatusStr() {
		return OrderStatusStr;
	}

	public void setOrderStatusStr(String orderStatusStr) {
		OrderStatusStr = orderStatusStr;
	}

	public String getLight() {
		return Light;
	}

	public void setLight(String light) {
		Light = light;
	}

	public String getOrderNumber() {
		return OrderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		OrderNumber = orderNumber;
	}

	public String getCustomerID() {
		return CustomerID;
	}

	public void setCustomerID(String customerID) {
		CustomerID = customerID;
	}

	public String getSalesChannelType() {
		return SalesChannelType;
	}

	public void setSalesChannelType(String salesChannelType) {
		SalesChannelType = salesChannelType;
	}

	public String getOrderDate() {
		return OrderDate;
	}

	public void setOrderDate(String orderDate) {
		OrderDate = orderDate;
	}

	public double getGrandTotal() {
		return GrandTotal;
	}

	public void setGrandTotal(double grandTotal) {
		GrandTotal = grandTotal;
	}

	public double getShippingFee() {
		return ShippingFee;
	}

	public void setShippingFee(double shippingFee) {
		ShippingFee = shippingFee;
	}
	
	public double getAllDiscountTotal() {
		return AllDiscountTotal;
	}

	public void setAllDiscountTotal(double allDiscountTotal) {
		AllDiscountTotal = allDiscountTotal;
	}

	public String getCurrencyCodeStr() {
		return CurrencyCodeStr;
	}

	public void setCurrencyCodeStr(String currencyCodeStr) {
		CurrencyCodeStr = currencyCodeStr;
	}

	public String getCurrencyCode() {
		return CurrencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		CurrencyCode = currencyCode;
	}

	public String getOrderStatus() {
		return OrderStatus;
	}

	public void setOrderStatus(String orderStatus) {
		OrderStatus = orderStatus;
	}

	public String getPaymentStatus() {
		return PaymentStatus;
	}

	public void setPaymentStatus(String paymentStatus) {
		PaymentStatus = paymentStatus;
	}

	public String getLockStatus() {
		return LockStatus;
	}

	public void setLockStatus(String lockStatus) {
		LockStatus = lockStatus;
	}

	public String getShippingMethod() {
		return ShippingMethod;
	}

	public void setShippingMethod(String shippingMethod) {
		ShippingMethod = shippingMethod;
	}

	public ShipAddress getShippingAddress() {
		return ShippingAddress;
	}

	public void setShippingAddress(ShipAddress shippingAddress) {
		ShippingAddress = shippingAddress;
	}

	public List<Liness> getLines() {
		return Lines;
	}

	public void setLines(List<Liness> lines) {
		Lines = lines;
	}

	public List<Liness> getGiftLines() {
		return giftLines;
	}

	public void setGiftLines(List<Liness> giftLines) {
		this.giftLines = giftLines;
	}

	public boolean isCanCancel() {
		return CanCancel;
	}

	public void setCanCancel(boolean canCancel) {
		CanCancel = canCancel;
	}

	public boolean isCanComplete() {
		return CanComplete;
	}

	public void setCanComplete(boolean canComplete) {
		CanComplete = canComplete;
	}

	public boolean isCanModifyShippingAddress() {
		return CanModifyShippingAddress;
	}

	public void setCanModifyShippingAddress(boolean canModifyShippingAddress) {
		CanModifyShippingAddress = canModifyShippingAddress;
	}

	public boolean isCanBeginEdit() {
		return CanBeginEdit;
	}

	public void setCanBeginEdit(boolean canBeginEdit) {
		CanBeginEdit = canBeginEdit;
	}

	public boolean isCanEndEdit() {
		return CanEndEdit;
	}

	public void setCanEndEdit(boolean canEndEdit) {
		CanEndEdit = canEndEdit;
	}

	/**
	 * json to model
	 * 
	 * @param jsonString
	 * @return [参数说明]
	 * 
	 * @return OrderModel [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public OrderModel json2model(String jsonString) {
		if (null == jsonString || "".equals(jsonString.trim())) {
			return null;
		}

		try {
			return json2model(new JSONObject(jsonString));
		} catch (JSONException e) {
		}

		return null;

	}

	/**
	 * json to model
	 * 
	 * @param json
	 * @return [参数说明]
	 * 
	 * @return OrderModel [返回类型说明]
	 * @throws JSONException 
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public OrderModel json2model(JSONObject json) throws JSONException {
		if (null == json) {
			return null;
		}
		OrderModel model = new OrderModel();
		/**
		 * 订单基本信息
		 */
		if (json.optString(json.optString(ServerConstant.Order.ORDERNUMBER)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERNUMBER)) {
			model.setOrderNumber(json
					.optString(ServerConstant.Order.ORDERNUMBER));
		}
		if (json.optString(json.optString(ServerConstant.Order.CUSTOMERID)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.CUSTOMERID)) {
			model.setCustomerID(json.optString(ServerConstant.Order.CUSTOMERID));
		}
		if (json.optString(json
				.optString(ServerConstant.Order.SALESCHANNELTYPE)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.SALESCHANNELTYPE)) {
			model.setSalesChannelType(json
					.optString(ServerConstant.Order.SALESCHANNELTYPE));
		}
		if (json.optString(json.optString(ServerConstant.Order.ORDERDATE)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERDATE)) {
			model.setOrderDate(json.optString(ServerConstant.Order.ORDERDATE));
		}
		model.setGrandTotal(json.optDouble(ServerConstant.Order.GRANDTOTAL));

		model.setShippingFee(json.optDouble(ServerConstant.Order.SHIPPINGFEE));
		
		model.setAllDiscountTotal(json.optDouble(ServerConstant.Order.ALL_DISCOUNT_TOTAL));

		String currencyCode = json
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE);
		if (currencyCode != JSONObject.NULL
				&& !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE)) {
			model.setCurrencyCode(currencyCode);
		}

		String currencyCodeStr = json
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE_STR);
		if (currencyCodeStr != JSONObject.NULL
				&& !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE_STR)) {
			model.setCurrencyCodeStr(currencyCodeStr);
		}

		if (json.optString(json.optString(ServerConstant.Order.ORDERSTATUS)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERSTATUS)) {
			model.setOrderStatus(json
					.optString(ServerConstant.Order.ORDERSTATUS));
		}
		if (json.optString(json.optString(ServerConstant.Order.PAYMENTSTATUS)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.PAYMENTSTATUS)) {
			model.setPaymentStatus(json
					.optString(ServerConstant.Order.PAYMENTSTATUS));
		}
		if (json.optString(json.optString(ServerConstant.Order.LOCKSTATUS)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.LOCKSTATUS)) {
			model.setLockStatus(json.optString(ServerConstant.Order.LOCKSTATUS));
		}
		if (json.optString(json.optString(ServerConstant.Order.SHIPPINGMETHOD)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.SHIPPINGMETHOD)) {
			model.setShippingMethod(json
					.optString(ServerConstant.Order.SHIPPINGMETHOD));
		}
		if (json.optString(json.optString(ServerConstant.Order.LIGHT)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.LIGHT)) {
			model.setLight(json.optString(ServerConstant.Order.LIGHT));
		}
		if (json.optString(json.optString(ServerConstant.Order.ORDERSTATUSSTR)) != JSONObject.NULL
				&& !json.isNull(ServerConstant.Order.ORDERSTATUSSTR)) {
			model.setOrderStatusStr(json
					.optString(ServerConstant.Order.ORDERSTATUSSTR));
		}

		model.setCanBeginEdit(json
				.optBoolean(ServerConstant.Order.CAN_BEGINEDIT));

		model.setCanCancel(json.optBoolean(ServerConstant.Order.CAN_CANCEL));
		model.setCanComplete(json.optBoolean(ServerConstant.Order.CAN_COMPLETE));
		model.setCanEndEdit(json.optBoolean(ServerConstant.Order.CAN_ENDEDIT));
		model.setCanModifyShippingAddress(json
				.optBoolean(ServerConstant.Order.CAN_MODIFYSHIPPINGADDRESS));
		model.setIsPaymentCompleted(json
				.optBoolean(ServerConstant.Order.IS_PAYMENT_COMPLETED));
		model.setCanRepay(json.optBoolean(ServerConstant.Order.CANREPAY));
		model.setDueAmount(json.optDouble(ServerConstant.Order.DUEAMOUNT));
		model.setIsCompleted(json.optBoolean(ServerConstant.Order.IS_COMPLETED));
		model.setIsCancelled(json.optBoolean(ServerConstant.Order.IS_CANCELLED));
		model.setIsFullShipped(json
				.optBoolean(ServerConstant.Order.IS_FULLSHIPPED));
		model.setIsShipping(json.optBoolean(ServerConstant.Order.IS_SHIPPING));
		model.setExpectReturnSCAmount((float)json.optDouble("ExpectReturnSCAmount"));
		
		/**
		 * 订单地址信息
		 */
		ShipAddress shipAdd = new ShipAddress();
		JSONObject shippingaddress = json
				.optJSONObject(ServerConstant.Order.SHIPPINGADDRESS);
		if (shippingaddress.optString(ServerConstant.Order.FIRSTNAME) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.FIRSTNAME)) {
			shipAdd.setFirstName(shippingaddress
					.optString(ServerConstant.Order.FIRSTNAME));
		}
		if (shippingaddress.optString(ServerConstant.Order.LASTNAME) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.LASTNAME)) {
			shipAdd.setLastName(shippingaddress
					.optString(ServerConstant.Order.LASTNAME));
		}
		if (shippingaddress.optString(ServerConstant.Order.PHONENUMBER) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.PHONENUMBER)) {
			shipAdd.setPhoneNumber(shippingaddress
					.optString(ServerConstant.Order.PHONENUMBER));
		}
		if (shippingaddress.optString(ServerConstant.Order.PHONE) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.PHONE)) {
			shipAdd.setPhone(shippingaddress
					.optString(ServerConstant.Order.PHONE));
		}
		if (shippingaddress.optString(ServerConstant.Order.POSTALCODE) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.POSTALCODE)) {
			shipAdd.setPostalCode(shippingaddress
					.optString(ServerConstant.Order.POSTALCODE));
		}
		if (shippingaddress.optString(ServerConstant.Order.STREET1) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.STREET1)) {
			shipAdd.setStreet1(shippingaddress
					.optString(ServerConstant.Order.STREET1));
		}
		if (shippingaddress.optString(ServerConstant.Order.STREET2) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.STREET2)) {
			shipAdd.setStreet2(shippingaddress
					.optString(ServerConstant.Order.STREET2));
		}
		if (shippingaddress.optString(ServerConstant.Order.CITY) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.CITY)) {
			shipAdd.setCity(shippingaddress
					.optString(ServerConstant.Order.CITY));
		}
		if (shippingaddress.optString(ServerConstant.Order.STATE) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.STATE)) {
			shipAdd.setState(shippingaddress
					.optString(ServerConstant.Order.STATE));
		}
		if (shippingaddress.optString(ServerConstant.Order.COUNTRY) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.COUNTRY)) {
			shipAdd.setCountry(shippingaddress
					.optString(ServerConstant.Order.COUNTRY));
		}
		if (shippingaddress.optString(ServerConstant.Order.PHONENUMBER) != JSONObject.NULL
				&& !shippingaddress.isNull(ServerConstant.Order.PHONENUMBER)) {
			shipAdd.setMobile(shippingaddress
					.optString(ServerConstant.Order.PHONENUMBER));
		}
		model.setShippingAddress(shipAdd);
		/**
		 * 订单其他信息
		 */
		List<Liness> list = new ArrayList<Liness>();
		Liness liness = null;
		try {
			JSONArray js = json.optJSONArray(ServerConstant.Order.LINES);
			for (int i = 0; i < js.length(); i++) {
				liness = new Liness();
				JSONObject linesObject = js.getJSONObject(i);
				liness.setGiftQty(js.getJSONObject(i).optInt(
						ServerConstant.Order.GIFTQTY));
				if (linesObject.optString(ServerConstant.Order.NOTES) != JSONObject.NULL
						&& !linesObject.isNull(ServerConstant.Order.NOTES)) {
					liness.setNotes(linesObject
							.optString(ServerConstant.Order.NOTES));
				}
				liness.setSku(linesObject.optInt(ServerConstant.Order.SKU));
				liness.setOrderedQty(linesObject
						.optInt(ServerConstant.Order.ORDEREDQTY));
				liness.setUnitCost(linesObject
						.optDouble(ServerConstant.Order.UNITCOST));
				liness.setUnitPrice(linesObject
						.optDouble(ServerConstant.Order.UNITPRICE));
				if (linesObject.optString(ServerConstant.Order.NAME) != JSONObject.NULL
						&& !linesObject.isNull(ServerConstant.Order.NAME)) {
					liness.setName(linesObject
							.optString(ServerConstant.Order.NAME));
				}
				if (linesObject.optString(ServerConstant.Order.SMILEIMAGEURL) != JSONObject.NULL
						&& !linesObject
								.isNull(ServerConstant.Order.SMILEIMAGEURL)) {
					liness.setSmileImageUrl(linesObject
							.optString(ServerConstant.Order.SMILEIMAGEURL));
				}
				liness.setGift(false);
				list.add(liness);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		model.setLines(list);
		/**
		 * 赠送sku
		 */
		try {
			List<Liness> giftSkus = new ArrayList<Liness>();
			String jsonString = json.getString(ServerConstant.Order.GIFT_LINES);
			if (null != jsonString && !"null".equals(jsonString)) {
				JSONArray giftArray = json
						.optJSONArray(ServerConstant.Order.GIFT_LINES);
				if (null != giftArray && giftArray.length() > 0) {
					for (int i = 0; i < giftArray.length(); i++) {
						Liness giftLines = new Liness();
						JSONObject giftObject = giftArray.getJSONObject(i);
						if (giftObject.optString(ServerConstant.Order.NOTES) != JSONObject.NULL
								&& !giftObject
										.isNull(ServerConstant.Order.NOTES)) {
							giftLines.setNotes(giftObject
									.optString(ServerConstant.Order.NOTES));
						}
						giftLines.setSku(giftObject
								.optInt(ServerConstant.Order.SKU));
						giftLines.setGiftQty(giftObject
								.optInt(ServerConstant.Order.GIFTQTY));
						giftLines.setUnitCost(giftObject
								.optDouble(ServerConstant.Order.UNITCOST));
						giftLines.setUnitPrice(giftObject
								.optDouble(ServerConstant.Order.UNITPRICE));
						if (giftObject.optString(ServerConstant.Order.NAME) != JSONObject.NULL
								&& !giftObject
										.isNull(ServerConstant.Order.NAME)) {
							giftLines.setName(giftObject
									.optString(ServerConstant.Order.NAME));
						}
						if (giftObject
								.optString(ServerConstant.Order.SMILEIMAGEURL) != JSONObject.NULL
								&& !giftObject
										.isNull(ServerConstant.Order.SMILEIMAGEURL)) {
							giftLines
									.setSmileImageUrl(giftObject
											.optString(ServerConstant.Order.SMILEIMAGEURL));
						}
						giftLines.setGift(true);
						giftSkus.add(giftLines);
					}
					model.setGiftLines(giftSkus);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		//设置应收运费及运费折扣
		model.setReceivableShippingFee(json.optDouble(ServerConstant.Order.RECEIVABLESHIPPINGFEE));
		model.setShippingFeeDiscount(json.optDouble(ServerConstant.Order.SHIPPINGFEEDISCOUNT));
		
		//增加物流信息
		if (json.optJSONArray(ServerConstant.Order.PACKAGES) != null
				&& !json.isNull(ServerConstant.Order.PACKAGES)) {
			ArrayList<Package> packageList = new ArrayList<Package>();
			JSONArray pakageArray = json.optJSONArray(ServerConstant.Order.PACKAGES);
			for (int i = 0; i < pakageArray.length(); i++) {
				Package pack = new Package();
				pack = pack.json2Object((JSONObject)pakageArray.get(i));
				if(pack!=null){
					packageList.add(pack);
				}
			}
			model.setPackages(packageList);
		}
		return model;
	}

	/**
	 * model to json
	 * 
	 * @return [参数说明]
	 * @return JSONObject [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public JSONObject tojson() {
		JSONObject json = new JSONObject();
		try {

			if (null != OrderNumber) {
				json.put(ServerConstant.Order.ORDERNUMBER, OrderNumber);
			}
			if (null != CustomerID) {
				json.put(ServerConstant.Order.CUSTOMERID, CustomerID);
			}
			if (null != SalesChannelType) {
				json.put(ServerConstant.Order.SALESCHANNELTYPE,
						SalesChannelType);
			}

			if (null != Light) {
				json.put(ServerConstant.Order.LIGHT, Light);
			}

			if (null != OrderDate) {
				json.put(ServerConstant.Order.ORDERDATE, OrderDate);
			}

			json.put(ServerConstant.Order.GRANDTOTAL, GrandTotal);

			if (null != CurrencyCode) {
				json.put(ServerConstant.Order.CURRENCYCODE, CurrencyCode);
			}
			if (null != ShippingAddress.FirstName) {
				json.put(ServerConstant.Order.FIRSTNAME,
						ShippingAddress.FirstName);
			}
			if (null != ShippingAddress.LastName) {
				json.put(ServerConstant.Order.LASTNAME,
						ShippingAddress.LastName);
			}
			if (null != ShippingAddress.PhoneNumber) {
				json.put(ServerConstant.Order.PHONENUMBER,
						ShippingAddress.PhoneNumber);
			}
			if (null != ShippingAddress.Phone) {
				json.put(ServerConstant.Order.PHONE, ShippingAddress.Phone);
			}
			if (null != ShippingAddress.PostalCode) {
				json.put(ServerConstant.Order.POSTALCODE,
						ShippingAddress.PostalCode);
			}
			if (null != ShippingAddress.Street1) {
				json.put(ServerConstant.Order.STREET1, ShippingAddress.Street1);
			}
			if (null != ShippingAddress.Street2) {
				json.put(ServerConstant.Order.STREET2, ShippingAddress.Street2);
			}
			if (null != ShippingAddress.City) {
				json.put(ServerConstant.Order.CITY, ShippingAddress.City);
			}
			if (null != ShippingAddress.State) {
				json.put(ServerConstant.Order.STATE, ShippingAddress.State);
			}
			if (null != ShippingAddress.Country) {
				json.put(ServerConstant.Order.COUNTRY, ShippingAddress.Country);
			}

			for (int i = 0; i < Lines.size(); i++) {
				json.put(ServerConstant.Order.UNITCOST, Lines.get(i).UnitCost);

				json.put(ServerConstant.Order.UNITPRICE, Lines.get(i).UnitPrice);
				if (null != Lines.get(i).Name) {
					json.put(ServerConstant.Order.NAME, Lines.get(i).Name);
				}
				if (null != Lines.get(i).SmileImageUrl) {
					json.put(ServerConstant.Order.SMILEIMAGEURL,
							Lines.get(i).SmileImageUrl);
				}
				if (0 != Lines.get(i).Sku) {
					json.put(ServerConstant.Order.SKU, Lines.get(i).Sku);
				}

				json.put(ServerConstant.Order.UNITCOST, Lines.get(i).UnitCost);

				json.put(ServerConstant.Order.UNITPRICE, Lines.get(i).UnitPrice);
				if (null != Lines.get(i).Name) {
					json.put(ServerConstant.Order.NAME, Lines.get(i).Name);
				}
				if (null != Lines.get(i).SmileImageUrl) {
					json.put(ServerConstant.Order.SMILEIMAGEURL,
							Lines.get(i).SmileImageUrl);
				}
			}

			if (null != OrderStatus) {
				json.put(ServerConstant.Order.ORDERSTATUS, OrderStatus);
			}
			if (null != PaymentStatus) {
				json.put(ServerConstant.Order.PAYMENTSTATUS, PaymentStatus);
			}
			if (null != LockStatus) {
				json.put(ServerConstant.Order.LOCKSTATUS, LockStatus);
			}
			if (null != ShippingMethod) {
				json.put(ServerConstant.Order.SHIPPINGMETHOD, ShippingMethod);
			}

		} catch (JSONException e) {
		}

		return json;
	}

	@Override
	public String toString() {
		return tojson().toString();
	}
}
